import Vue from "vue";
import Router from "vue-router";

Vue.use(Router);

/* Layout */
import Layout from "@/layout";

/**
 * constantRoutes
 * a base page that does not have permission requirements
 * all roles can be accessed
 */
export const constantRoutes = [
  // {
  //   path: "/redirect",
  //   component: Layout,
  //   hidden: true,
  //   children: [
  //     {
  //       path: "/redirect/:path(.*)",
  //       component: () => import("@/views/redirect/index"),
  //     },
  //   ],
  // },
  {
    path: "/",
    component: Layout,
    redirect: "/Goods/index",
    children: [
      {
        path: "index",
        component: () => import("@/views/Goods/Index"),
        name: "Goods",
        meta: { title: "商品管理", icon: "el-icon-s-goods" },
      },
    ],
  },
  {
    path: "/Goods",
    component: Layout,
    redirect: "/Goods/index",
    hidden: true,
    // alwaysShow: true,
    meta: { title: "商品管理", icon: "el-icon-s-goods" },
    children: [
      {
        path: "index",
        component: () => import("@/views/Goods/Index"),
        name: "Goods",
        meta: { title: "商品管理", icon: "el-icon-s-goods" },
      },
    ],
  },
  {
    path: "/Users",
    component: Layout,
    redirect: "/Users/index",
    // alwaysShow: true,
    meta: { title: "用户管理", icon: "el-icon-user" },
    children: [
      {
        path: "index",
        component: () => import("@/views/Users/Index"),
        name: "Users",
        meta: { title: "用户管理", icon: "el-icon-user" },
      },
    ],
  },
  {
    path: "/login",
    component: () => import("@/views/login/index"),
    hidden: true,
  },
];

/**
 * asyncRoutes
 * the routes that need to be dynamically loaded based on user roles
 */
export const asyncRoutes = [
  // 404 page must be placed at the end !!!
  { path: "*", redirect: "/404", hidden: true },
];

const createRouter = () =>
  new Router({
    // mode: 'history', // require service support
    scrollBehavior: () => ({ y: 0 }),
    routes: constantRoutes,
  });

const router = createRouter();

// Detail see: https://github.com/vuejs/vue-router/issues/1234#issuecomment-357941465
export function resetRouter() {
  const newRouter = createRouter();
  router.matcher = newRouter.matcher; // reset router
}

export default router;
